package cn.szzsi.common.model;

import cn.szzsi.common.model.base.BaseShipping;
import com.jfinal.kit.StrKit;

import java.util.Date;
import java.util.List;

/**
 * 实体类 - 发货
 * 
 */
public class Shipping extends BaseShipping<Shipping> {

	private static final long serialVersionUID = -7846935939597029639L;

	public static final Shipping dao = new Shipping().dao();
	
	// 重写save方法
	public boolean save(Shipping shipping){
		shipping.set("id", StrKit.getRandomUUID());
		shipping.set("createDate", new Date());
		return shipping.save();
	}
		
	// 物流项
	public List<DeliveryItem> getDeliveryItem(){
		String sql = "select * from mall_delivery_item where shipping_id = ?";
		return DeliveryItem.dao.find(sql,getStr("id"));
	}
	
	// 订单
	public Orders getOrder(){
		return Orders.dao.findById(getStr("order_id"));
	}
	
	/**
	 * 获取最后生成的发货编号
	 * 
	 * @return 发货编号
	 */
	public String getLastShippingSn() {
		String sql = "select shippingSn from mall_shipping  order by shippingSn desc";
		Shipping shippingList =  dao.findFirst(sql);
		if (shippingList != null) {
			return shippingList.getStr("shippingSn");
		} else {
			return null;
		}
	}

	/**
	 *
	 * @param orderId 订单编号
	 * @param delverSn 物流编号
	 */
	public Shipping getByOrderIdAndDeliverSn(String orderId, String delverSn){
		return dao.findFirst("select * from mall_shipping where order_id=? and deliverySn=?",orderId,delverSn);
	}

	/**
	 *
	 * @param orderId 订单编号
	 */
	public Shipping getByOrderId(String orderId){
		return dao.findFirst("select * from mall_shipping where order_id=?",orderId);
	}

}
